﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common.Extensions/EnumExtension 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       EnumExtension
* 创建时间：  2025/5/22 15:44:54
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public static class EnumExtension
    {
        /// <summary>
        ///用于确定对象是否等于所提供的任何值
        /// </summary>
        /// <param name="this">The object to be compared.</param>
        /// <param name="values">The value list to compare with the object.</param>
        /// <returns>true if the values list contains the object, else false.</returns>
        public static bool In(this Enum @this, params Enum[] values)
        {
            return Array.IndexOf(values, @this) >= 0;
        }

        /// <summary>
        /// An object extension method that gets description attribute.
        /// </summary>
        /// <param name="value">The value to act on.</param>
        /// <returns>The description attribute.</returns>
        public static string GetDescription(this Enum value)
        {
            if (value == null) return string.Empty;
            var attr = value.GetType().GetField(value.ToString())
                .GetCustomAttribute<DescriptionAttribute>();
            return attr?.Description;
        }
        /// <summary>
        /// 获取枚举的自定义属性
        /// </summary>
        /// <typeparam name="T">自定义属性类型</typeparam>
        /// <param name="value">枚举</param>
        /// <returns>返回自定义属性</returns>
        public static T GetAttribute<T>(this Enum value) where T : Attribute
        {
            FieldInfo field = value.GetType().GetField(value.ToString());
            return field.GetCustomAttribute(typeof(T)) as T;

        }
        /// <summary>
        /// 获取枚举的值
        /// </summary>
        /// <param name="value">枚举</param>
        /// <returns>返回枚举的值</returns>
        public static int GetValue(this Enum value)
        {
            return Convert.ToInt32(value);
        }

        /// <summary>
        /// 获取枚举所有的值对象
        /// </summary>
        /// <param name="type">枚举</param>
        /// <returns>返回枚举的值对象</returns>
        public static List<Tuple<string, string>> GetEnumSource(this Type type)
        {
            if (!type.GetTypeInfo().IsEnum)
                throw new Exception("type 类型必须为枚举类型!");

            var list = new List<Tuple<string, string>>();

            foreach (var value in Enum.GetValues(type))
            {
                var fieldName = Enum.GetName(type, value);
                var field = type.GetField(fieldName);
                var display = field.GetCustomAttribute(typeof(DescriptionAttribute)) as DescriptionAttribute;
                if (display != null)
                    list.Add(new Tuple<string, string>(Convert.ToInt32(value) + "", display.Description));
                else
                    list.Add(new Tuple<string, string>(Convert.ToInt32(value) + "", fieldName));
            }
            return list;
        }

        public static string ToDescription(this Enum item, bool useName = true)
        {
            string name = item.ToString();
            var desc = item.GetType().GetField(name)?.GetCustomAttribute<DescriptionAttribute>(false);
            return useName ? (desc?.Description ?? name) : desc?.Description;
        }

        public static string ToNameWithDescription(this Enum item)
        {
            string name = item.ToString();
            var desc = item.GetType().GetField(name)?.GetCustomAttribute<DescriptionAttribute>(false);
            return $"{name}{(desc == null || desc.Description.IsNull() ? "" : $"({desc?.Description})")}";
        }

        public static long ToInt64(this Enum item)
        {
            return Convert.ToInt64(item);
        }

        public static List<Dictionary<string, object>> ToList(this Enum value, bool ignoreNull = false)
        {
            var enumType = value.GetType();

            if (!enumType.IsEnum)
                return null;

            return Enum.GetValues(enumType).Cast<Enum>()
                .Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary<string, object>
                {
                    ["Label"] = x.ToDescription(),
                    ["Value"] = x
                }).ToList();
        }

        public static List<Dictionary<string, object>> ToList<T>(bool ignoreNull = false)
        {
            var enumType = typeof(T);

            if (!enumType.IsEnum)
                return null;

            return Enum.GetValues(enumType).Cast<Enum>()
                 .Where(m => !ignoreNull || !m.ToString().Equals("Null")).Select(x => new Dictionary<string, object>
                 {
                     ["Label"] = x.ToDescription(),
                     ["Value"] = x
                 }).ToList();
        }
    }
}
